package com.bci.pwtz.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/** 
 * @ClassName: Md5Util 
 * @Description: MD5加密算法工具类
 * @author xuds 
 * @date 2011-12-29 下午4:25:26 
 * 
*/
public class Md5Util {
	private static MessageDigest digest = null;

	/**
	 * MD5加密算法
	 * @param data
	 * @return
	 */
	public synchronized static final String hash(String data) {
		if (digest == null) {
			try {
				digest = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException nsae) {
				System.err.println("Failed to load the MD5 MessageDigest. "
						+ "Jive will be unable to function normally.");
				nsae.printStackTrace();
			}
		}
		// Now, compute hash.
		digest.update(data.getBytes());
		return encodeHex(digest.digest());
	}

	public static final String encodeHex(byte[] bytes) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);
		int i;

		for (i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buf.toString();
	}

	public static String test() {
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(Md5Util.hash("abc"));
	}

}
